class Solution1 {
    public char[][] updateBoard(char[][] board, int[] click) {
        int x=click[0],y=click[1];
        if(board[x][y]=='M'){
            board[x][y]='X';
            return board;
        }
        dfs(board,x,y);
        return board;
    }

    public int[] dx={-1,-1,-1,0,1,1,1,0};
    public int[] dy={-1,0,1,1,1,0,-1,-1};

    private void dfs(char[][] board, int a, int b) {
        int m=board.length;
        int n=board[0].length;
        int sum=0;
        for(int i=0;i<8;i++){
            int x=a+dx[i];
            int y=b+dy[i];

            if(x>=0 && x<m && y>=0 && y<n && board[x][y]=='M'){
                sum++;
            }
        }
        if(sum==0){
            board[a][b]='B';
            for(int i=0;i<8;i++){
                int x=a+dx[i];
                int y=b+dy[i];

                if(x>=0 && x<m && y>=0 && y<n && board[x][y]=='E'){
                    dfs(board,x,y);
                }
            }
        }else{
            board[a][b]=(char)(sum+'0');
        }
    }
}